home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’87 / Source ƒ.sit / Source ƒ / C ƒ / TRANS-LSC / TransEdit.c < prev    next >
Encoding:
Text File  |  1986-11-12  |  37.7 KB  |  1,729 lines  |  [TEXT/KAHL]

  1. /*
  2.     TransEdit.c version 1.0 - TransSkel plug-in module supporting an
  3.     arbitrary number of generic edit windows.  Each window may be
  4.     bound to a file.
  5.  
  6.     *** Requires FakeAlert.c for proper linking! ***
  7.  
  8.     Shortcomings:
  9.         Doesn't check for the obvious out of memory conditions.
  10.     
  11.     TransSkel and TransEdit are public domain, and are written by:
  12.  
  13.             Paul DuBois
  14.             Wisconsin Regional Primate Research Center
  15.             1220 Capital Court
  16.             Madison WI  53706  USA
  17.  
  18.     UUCP:    {allegra,ihnp4,seismo}!uwvax!uwmacc!dubois
  19.     ARPA:    dubois@unix.macc.wisc.edu
  20.             dubois@rhesus.primate.wisc.edu
  21.  
  22.     This version of TransEdit written for LightspeedC.  LightspeedC
  23.     is a trademark of:
  24.             THINK Technologies, Inc
  25.             420 Bedford Street  Suite 350
  26.             Lexington, MA  02173  USA
  27.  
  28.   History
  29.   08/25/86    Genesis.  Beta version.
  30.   09/15/86    Changed to allow arbitrary number of windows.
  31.   11/04/86    Added conditional stuff to allow compilation in
  32.              single- or multiple-window mode.  Changed version to 1.0.
  33. */
  34.  
  35.  
  36. /*
  37.     The following symbol controls the compile mode.  If it is #define'd,
  38.     TransEdit allows only a single edit window, and generates less code.
  39.     If it is #undef'ed, TransEdit allows an arbitrary number of edit
  40.     windows, but generates more code.
  41. */
  42.  
  43. # undef    singleEdit
  44.  
  45.  
  46. # include    <ControlMgr.h>    /* includes WindowMgr.h, QuickDraw.h, MacTypes.h */
  47. # include    <StdFilePkg.h>
  48. # include    <FileMgr.h>
  49. # include    <ToolBoxUtil.h>
  50. # include    "TransEdit.h"
  51.  
  52.  
  53. /*
  54.     Edit window types, constants, variables.
  55. */
  56.  
  57.  
  58. # define    enter            3
  59. # define    cr                13
  60. # define    monaco            4
  61. # define    shiftKey        0x0200
  62.  
  63.  
  64. typedef enum            /* Edit menu item numbers */
  65. {
  66.     undo = 1,
  67.     /* --- */
  68.     cut = 3,
  69.     copy,
  70.     paste,
  71.     clear        /* (it's ok if the host doesn't have this item) */
  72. };
  73.  
  74.  
  75. /*
  76.     Default values for edit window text display characteristics
  77.     and event notification procedures
  78. */
  79.  
  80. static int        e_font = monaco;    /* default font                 */
  81. static int        e_size = 9;            /* default pointsize            */
  82. static int        e_wrap = 0;            /* default word wrap (on)       */
  83. static int        e_just = teJustLeft;/* default justification        */
  84. static ProcPtr    e_key = nil;        /* default key procedure        */
  85. static ProcPtr    e_activate = nil;    /* default activation procedure */
  86. static ProcPtr    e_close = nil;        /* default close procedure      */
  87.  
  88.  
  89. # ifndef    singleEdit
  90.  
  91. /*
  92.     New(TypeName) returns handle to new object, for any TypeName.
  93.     If there is insufficient memory, the result is nil.
  94. */
  95.  
  96. # define    New(x)    (x **) NewHandle ((Size) sizeof (x))
  97.  
  98.  
  99. /*
  100.     ewList points to a list of structures describing the known edit
  101.     windows.
  102. */
  103.  
  104.  
  105. typedef struct EditInfo
  106. {
  107.     WindowPtr        editWind;    /* the edit window                   */
  108.     Boolean            bound;        /* whether window is bound to file   */
  109.     SFReply            editFile;    /* file it's bound to, if bound true */
  110.     TEHandle        editTE;        /* window text                       */
  111.     Boolean            dirty;        /* whether text modified since save  */
  112.     ControlHandle    scroll;        /* scroll bar                        */
  113.     int                visLines;    /* # lines visible in window, max    */
  114.     ProcPtr            eKey;        /* key click notifier                */
  115.     ProcPtr            eActivate;    /* activate event notifier           */
  116.     ProcPtr            eClose;        /* close notifier                    */
  117.     struct EditInfo    **eNext;    /* next information structure        */
  118. } EditInfo, *EIPtr, **EIHandle;
  119.  
  120.  
  121. static EIHandle        ewList = nil;
  122.  
  123. # endif
  124.  
  125.  
  126. /*
  127.     Global variables - most of these are always synced to
  128.     the current window.  Note that not all these are set by
  129.     SyncGlobals, since some are not often needed.  When they
  130.     are all needed, use SyncAllGlobals.
  131. */
  132.  
  133. # ifndef    singleEdit
  134. static EIHandle            editInfo;        /* window's info structure      */
  135. # endif
  136.  
  137. static WindowPtr        editWind = nil;    /* the window                   */
  138. static TEHandle            editTE;            /* window text                  */
  139. static ControlHandle    editScroll;        /* the scroll bar               */
  140. static SFReply            editFile;        /* file information             */
  141. static int                visLines;        /* number of lines in window    */
  142. static Boolean            bound;            /* true if window bound to file */
  143. static Boolean            dirty;            /* whether window is dirty      */
  144. static ProcPtr            eKey;            /* key click notifier           */
  145. static ProcPtr            eActivate;        /* activate event notifier      */
  146. static ProcPtr            eClose;            /* close notifier               */
  147.  
  148.  
  149. static int        windID = 0;
  150. static Point    dlogWhere = { 70, 100 };    /* GetFile/PutFile location */
  151. static OSType    creator = 'TEDT';            /* default file creator */
  152.  
  153. static RgnHandle    clipRgn;
  154.  
  155.  
  156. /* -------------------------------------------------------------------- */
  157. /*                Miscellaneous Internal (private) Routines                */
  158. /* -------------------------------------------------------------------- */
  159.  
  160.  
  161. /*
  162.     Save and restore the current window's clip region
  163. */
  164.  
  165. static SaveClipRgn ()
  166. {
  167.     clipRgn = NewRgn ();
  168.     GetClip (clipRgn);
  169. }
  170.  
  171.  
  172. static RestoreClipRgn ()
  173. {
  174.     SetClip (clipRgn);
  175.     DisposeRgn (clipRgn);
  176. }
  177.  
  178.  
  179. /*
  180.     Draw grow box in lower right hand corner of window.
  181. */
  182.  
  183.  
  184. static DrawGrowBox ()
  185. {
  186. Rect        r;
  187.  
  188.     SaveClipRgn ();
  189.     r = editWind->portRect;
  190.     r.left = r.right - 15;        /* draw only in corner */
  191.     r.top = r.bottom - 15;
  192.     ClipRect (&r);
  193.     DrawGrowIcon (editWind);
  194.     RestoreClipRgn ();
  195. }
  196.  
  197.  
  198. /* -------------------------------------------------------------------- */
  199. /*            Lowest-level Internal (Private) Edit Window Routines        */
  200. /* -------------------------------------------------------------------- */
  201.  
  202.  
  203. # ifndef    singleEdit
  204.  
  205. /*
  206.     Get edit window info associated with window.
  207.     Return nil if window isn't a known edit window.
  208. */
  209.  
  210. static EIHandle GetEInfo (theWind)
  211. WindowPtr    theWind;
  212. {
  213. register EIHandle    h;
  214.  
  215.     for (h = ewList; h != nil; h = (**h).eNext)
  216.     {
  217.         if ((**h).editWind == theWind)
  218.             return (h);
  219.     }
  220.     return (nil);
  221. }
  222.  
  223. # endif
  224.  
  225.  
  226. # ifdef    singleEdit
  227. # define    SyncAllGlobals    SyncGlobals
  228. # endif
  229.  
  230.  
  231. /*
  232.     Synchronize globals to an edit window and make it the
  233.     current port.  theWind must be a legal edit window, with one
  234.     exception:  if theWind is nil, the variables are synced to the
  235.     port that's already current.  That is safe (and correct) because:
  236.     (i)     nil is only passed by edit window handler procedures,
  237.          which are only attached to edit windows
  238.     (ii) TransSkel always sets the port to the window before
  239.          calling the handler proc.
  240.     Hence, using the current port under these circumstances always
  241.     produces a legal edit window.
  242. */
  243.  
  244. static SyncGlobals (theWind)
  245. WindowPtr    theWind;
  246. {
  247.  
  248.     if (theWind == nil)                    /* use current window */
  249.         GetPort (&theWind);
  250.  
  251.     SetPort (theWind);
  252.  
  253. # ifndef    singleEdit
  254.  
  255.     editWind = theWind;
  256.     editInfo = GetEInfo (editWind);
  257.     editTE = (**editInfo).editTE;
  258.     editScroll = (**editInfo).scroll;
  259.     visLines = (**editInfo).visLines;
  260.  
  261. # endif
  262.  
  263. }
  264.  
  265.  
  266. # ifndef    singleEdit
  267.  
  268. static SyncAllGlobals (theWind)
  269. WindowPtr    theWind;
  270. {
  271.  
  272.     SyncGlobals (theWind);                /* sync display globals */
  273.     editFile = (**editInfo).editFile;    /* sync file, state, and */
  274.     bound = (**editInfo).bound;            /* procedure globals */
  275.     dirty = (**editInfo).dirty;
  276.     eKey = (**editInfo).eKey;
  277.     eActivate = (**editInfo).eActivate;
  278.     eClose = (**editInfo).eClose;
  279. }
  280.  
  281. # endif
  282.  
  283.  
  284. /*
  285.     Set dirty flag for current window
  286. */
  287.  
  288. static SetDirty (boolVal)
  289. Boolean    boolVal;
  290. {
  291.  
  292. # ifdef    singleEdit
  293.     dirty = boolVal;
  294. # else
  295.     (**editInfo).dirty = boolVal;
  296. # endif
  297. }
  298.  
  299.  
  300. /* -------------------------------------------------------------------- */
  301. /*                    Internal (private) Display Routines                    */
  302. /* -------------------------------------------------------------------- */
  303.  
  304.  
  305. /*
  306.     Calculate the dimensions of the editing rectangle for
  307.     editWind (which must be set properly and is assumed to be
  308.     the current port).  (The viewRect and destRect are the
  309.     same size.)  Assumes the port, text font and text size are all
  310.     set properly.  The viewRect is sized so that an integral
  311.     number of lines can be displayed in it, i.e., so that a
  312.     partial line never shows at the bottom.  If that's not
  313.     done, funny things can happen to the caret.
  314. */
  315.  
  316. static GetEditRect (r)
  317. Rect    *r;
  318. {
  319. FontInfo        f;
  320. register int    lineHeight;
  321.  
  322.     GetFontInfo (&f);
  323.     lineHeight = f.ascent + f.descent + f.leading;
  324.     *r = editWind->portRect;
  325.     r->left += 4;
  326.     r->right -= 17;        /* leave room for scroll bar */
  327.     r->top += 2;
  328.     r->bottom = r->top + ((r->bottom - r->top - 2) / lineHeight) * lineHeight;
  329. }
  330.  
  331.  
  332. /*
  333.     Set the edit rect properly.
  334. */
  335.  
  336. static SetEditRect ()
  337. {
  338. Rect    r;
  339.  
  340.     GetEditRect (&r);
  341.     (**editTE).destRect.right = r.right;
  342.     (**editTE).viewRect = r;
  343. }
  344.  
  345.  
  346.  
  347. /*
  348.     Calculate the dimensions of the scroll bar rectangle for
  349.     editWind (which must be set properly).  Make sure that
  350.     the edges overlap the window frame and the grow box.
  351. */
  352.  
  353. static CalcScrollRect (r)
  354. Rect        *r;
  355. {
  356.     *r = editWind->portRect;
  357.     ++r->right;
  358.     --r->top;
  359.     r->left = r->right - 16;
  360.     r->bottom -= 14;
  361. }
  362.  
  363.  
  364. /*
  365.     Return true if the mouse is in the non-scrollbar part of the
  366.     edit window.
  367. */
  368.  
  369. static Boolean PtInText (pt)
  370. Point    pt;
  371. {
  372. Rect    r;
  373.  
  374.     r = editWind->portRect;
  375.     r.right -= 15;
  376.     return (PtInRect (pt, &r));
  377. }
  378.  
  379.  
  380. /*
  381.     Set the cursor appropriately.  If theCursor == iBeamCursor, check
  382.     that it's really in the text area of an edit window (and if not
  383.     set the cursor to an arrow instead).  Otherwise, set the cursor
  384.     to the given type (usually a watch).
  385.  
  386.     If the cursor is supposed to be set to an i-beam, it is assumed
  387.     that the globals are synced, because DoCursor changes them and
  388.     syncs them back.
  389.  
  390.     Pass -1 for theCursor to set the cursor to the arrow.
  391. */
  392.  
  393. static DoCursor (theCursor)
  394. int        theCursor;
  395. {
  396. Point    pt;
  397. GrafPtr    savePort;
  398.  
  399.     if (theCursor == iBeamCursor)            /* check whether there's an edit */
  400.     {                                        /* window in front and if so,    */
  401.         theCursor = -1;                        /* whether the cursor's in its   */
  402.         if (IsEWindow (FrontWindow ()))        /* text area                     */
  403.         {
  404.             GetPort (&savePort);
  405.             SyncGlobals (FrontWindow ());
  406.             GetMouse (&pt);
  407.             if (PtInText (pt))
  408.                 theCursor = iBeamCursor;
  409.             SyncGlobals (savePort);
  410.         }
  411.     }
  412.     SetCursor (theCursor == -1 ? &arrow : *GetCursor (theCursor));
  413. }
  414.  
  415.  
  416. /*
  417.     Calculate the number of lines currently scrolled off
  418.     the top.
  419. */
  420.  
  421. static LinesOffTop ()
  422. {
  423. register TEPtr    ePtr;
  424.  
  425.     ePtr = *editTE;
  426.     return (((*ePtr).viewRect.top - (*ePtr).destRect.top)
  427.                 / (*ePtr).lineHeight);
  428. }
  429.  
  430.  
  431. /*
  432.     Return the line number that the caret (or the beginning of
  433.     the currently selected text) is in.  Value returned is in
  434.     the range 0..(**editTE).nLines.  If = (**editTE).nLines, the
  435.     caret is past the last line.  The only special case to watch out
  436.     for is when the caret is at the very end of the text.  If the
  437.     last character is not a carriage return, then the caret is on
  438.     the (nLines-1)th line, not the (nLines)th line.
  439.  
  440.     (This really should do a binary search for speed.)
  441. */
  442.  
  443. static LineWithCaret ()
  444. {
  445. register int    i;
  446. register int    nLines;
  447. register int    teLength;
  448. register int    selStart;
  449. register int    lineStart;
  450.  
  451.     selStart = (**editTE).selStart;
  452.     nLines = (**editTE).nLines;
  453.     teLength = (**editTE).teLength;
  454.  
  455.     if (selStart == teLength)
  456.     {
  457.         if (teLength != 0 && (*((**editTE).hText))[teLength-1] != cr)
  458.             return (nLines - 1);
  459.         return (nLines);
  460.     }
  461.  
  462.     for (i = 0; /* empty */; ++i)
  463.     {
  464.         if ((lineStart = (**editTE).lineStarts[i]) >= selStart)
  465.         {
  466.             if (lineStart != selStart)
  467.                 --i;
  468.             return (i);
  469.         }
  470.     }
  471. }
  472.  
  473.  
  474. /*
  475.     Return the number of the last displayable line.  That's one
  476.     more than nLines if the text is empty or the last character
  477.     is a carriage return.
  478. */
  479.  
  480. static LastLine ()
  481. {
  482. register int    nLines;
  483. register int    teLength;
  484.  
  485.     nLines = (**editTE).nLines;
  486.     teLength = (**editTE).teLength;
  487.  
  488.     if (teLength == 0 || (*((**editTE).hText))[teLength-1] == cr)
  489.         nLines++;
  490.     return (nLines);
  491. }
  492.  
  493.  
  494. /*
  495.     Set the maximum value of the scroll bar.  It's set so that if
  496.     there's more text than fits in the window, the bottom line can
  497.     be scrolled up at least a little below the bottom of the window.
  498.  
  499.     The shenanigans with topLines and scrollableLines have to do with
  500.     the case where there may be less text than fills the window, but
  501.     most of it's scrolled off the top.  This can happen when you
  502.     scroll a bunch of stuff up, then delete everything visible in
  503.     the window.
  504. */
  505.  
  506. static SetScrollMax ()
  507. {
  508. register int    topLines;
  509. register int    scrollableLines;
  510. register int    max;
  511.  
  512.     topLines = LinesOffTop ();
  513.     scrollableLines = LastLine () - visLines;
  514.     max = (topLines > scrollableLines ? topLines : scrollableLines);
  515.  
  516.     if (max < 0)
  517.         max = 0;
  518.  
  519.     if (max != GetCtlMax (editScroll))
  520.     {
  521.         SetCtlMax (editScroll, max);
  522.         HiliteControl (editScroll, max > 0 ? 0 : 255);
  523.     }
  524. }
  525.  
  526.  
  527. /*
  528.     Set scroll bar current value (but only if it's different than
  529.     the current value, to avoid needless flashing).
  530. */
  531.  
  532. static SetScrollValue (value)
  533. int        value;
  534. {
  535.     if (GetCtlValue (editScroll) != value)
  536.         SetCtlValue (editScroll, value);
  537. }
  538.  
  539.  
  540. /*
  541.     Scroll to the correct position.  lDelta is the
  542.     amount to CHANGE the current scroll setting by.
  543. */
  544.  
  545. static ScrollText (lDelta)
  546. int        lDelta;
  547. {
  548. register int    topVisLine;
  549. register int    newTopVisLine;
  550.  
  551.     topVisLine = LinesOffTop ();
  552.     newTopVisLine = topVisLine + lDelta;
  553.     if (newTopVisLine < 0)                    /* clip to range */
  554.         newTopVisLine = 0;
  555.     if (newTopVisLine > GetCtlMax (editScroll))
  556.         newTopVisLine = GetCtlMax (editScroll);
  557.     SetScrollValue (newTopVisLine);
  558.     TEScroll (0, (topVisLine-newTopVisLine )*(**editTE).lineHeight, editTE);
  559. }
  560.  
  561.  
  562. /*
  563.     Scroll to home position without redrawing.
  564. */
  565.  
  566. static ScrollToHome ()
  567. {
  568. Rect                r;
  569.  
  570.     r = (**editTE).destRect;
  571.     OffsetRect (&r, 0, 2 - r.top);
  572.     (**editTE).destRect = r;
  573. }
  574.  
  575. /*
  576.     ClikLoop proc for autoscrolling text when the mouse is dragged out
  577.     of the text view rectangle.
  578.  
  579.     The clipping region has to be set to include the scroll bar,
  580.     because whenever this proc is called, TE has the region set down
  581.     to the view rectangle - if it's not reset, changes to the scroll
  582.     bar will not show up!
  583. */
  584.  
  585. static pascal Boolean AutoScroll ()
  586. {
  587. Point    p;
  588. Rect    r;
  589.  
  590.     SaveClipRgn ();
  591.     ClipRect (&editWind->portRect);
  592.     GetMouse (&p);
  593.     r = (**editTE).viewRect;
  594.     if (p.v < r.top)
  595.         ScrollText (-1);
  596.     else if (p.v > r.bottom)
  597.         ScrollText (1);
  598.     RestoreClipRgn ();
  599.     return (true);            /* true = 'keep tracking mouse' */
  600. }
  601.  
  602.  
  603. /*
  604.     Filter proc for tracking mousedown in scroll bar.  The code for
  605.     the part originally hit is shoved into the control's reference
  606.     value by Mouse() before this is called.
  607.  
  608.     I suspect odd scrolling may occur for hits in paging regions if
  609.     the window is allowed to size such that less than two lines show.
  610. */
  611.  
  612. static pascal void TrackScroll (theScroll, partCode)
  613. ControlHandle    theScroll;
  614. int                partCode;
  615. {
  616. register int    lDelta;
  617.  
  618.     if (partCode == GetCRefCon (theScroll))    /* still in same part? */
  619.     {
  620.         switch (partCode)
  621.         {
  622.             case inUpButton: lDelta = -1; break;
  623.             case inDownButton: lDelta = 1; break;
  624.             case inPageUp: lDelta = -(visLines - 1); break;
  625.             case inPageDown: lDelta = visLines - 1; break;
  626.         }
  627.         ScrollText (lDelta);
  628.     }
  629. }
  630.  
  631.  
  632. /*
  633.     Set the scroll bar properly and adjust the text in the
  634.     window so that the line containing the caret is visible.
  635.     If the line with the caret if more than a line outside of
  636.     the viewRect, try to place it in the middle of the window.
  637.  
  638.     Yes, it is necessary to SetScrollMax at the end.
  639. */
  640.  
  641. static AdjustDisplay ()
  642. {
  643. register int    caretLine;
  644. register int    topVisLine;
  645. register int    d;
  646.  
  647.     SetScrollMax ();
  648.     caretLine = LineWithCaret ();
  649.     topVisLine = LinesOffTop ();
  650.     if ((d = caretLine - topVisLine) < 0)
  651.         ScrollText (d == -1 ? -1 : d - visLines / 2);
  652.     else if (( d = caretLine - (topVisLine + visLines - 1)) > 0)
  653.         ScrollText (d == 1 ? 1 : d + visLines / 2);
  654.     else
  655.         SetScrollValue (topVisLine);
  656.     SetScrollMax ();    /* might have changed from scrolling */
  657. }
  658.  
  659.  
  660. /*
  661.     Overhaul the entire display.  This is called for major
  662.     catastrophes, such as resizing the window, or changes to
  663.     the word wrap style.  It makes sure the view and
  664.     destination rectangles are sized properly, and that the bottom
  665.     line of text never scrolls up past the bottom line of the
  666.     window, if there's enough to fill the window, and that the
  667.     scroll bar max and current values are set properly.
  668.  
  669.     Resizing the dest rect just means resetting the right edge
  670.     (the top is NOT reset), since text might be scrolled off the
  671.     top (i.e., destRect.top != 0).
  672.  
  673.     Doesn't redraw the control, though!
  674. */
  675.  
  676. static OverhaulDisplay (showCaret, recalc)
  677. Boolean    showCaret;
  678. Boolean    recalc;
  679. {
  680. Rect            r;
  681.  
  682.     r = (**editTE).viewRect;    /* erase current viewRect */
  683.     EraseRect (&r);
  684.     SetEditRect ();                /* recalculate editing rects */
  685.     if (recalc)
  686.         TECalText (editTE);            /* recalculate line starts */
  687.     visLines = ((**editTE).viewRect.bottom - (**editTE).viewRect.top)
  688.                     / (**editTE).lineHeight;
  689.  
  690. # ifndef    singleEdit
  691.     (**editInfo).visLines = visLines;
  692. # endif
  693.  
  694. /*
  695.     If there is text, but none of it is visible in the window
  696.     (it's all scrolled off the top), pull some down.
  697. */
  698.  
  699.     if (showCaret)
  700.         AdjustDisplay ();
  701.     else
  702.         SetScrollMax ();
  703.     r = (**editTE).viewRect;
  704.     TEUpdate (&r, editTE);
  705. }
  706.  
  707.  
  708. /* ---------------------------------------------------------------- */
  709. /*                        Window Handler Routines                        */
  710. /* ---------------------------------------------------------------- */
  711.  
  712.  
  713. /*
  714.     Handle mouse clicks in window.  The viewRect is never tested
  715.     directly, because if it were, clicks along the top, left and
  716.     bottom edges of the window wouldn't register.
  717. */
  718.  
  719. static Mouse (thePt, t, mods)
  720. Point    thePt;
  721. long    t;
  722. int        mods;
  723. {
  724. register int    thePart;
  725. register int    oldCtlValue;
  726.  
  727.     SyncGlobals (nil);        /* sync to current port */
  728.  
  729.     if ((thePart = TestControl (editScroll, thePt)) == inThumb)
  730.     {
  731.         oldCtlValue = GetCtlValue (editScroll);
  732.         if (TrackControl (editScroll, thePt, nil) == inThumb)
  733.             ScrollText (GetCtlValue (editScroll) - oldCtlValue);
  734.     }
  735.     else if (thePart != 0)
  736.     {
  737.         SetCRefCon (editScroll, (long) thePart);
  738.         (void) TrackControl (editScroll, thePt, &TrackScroll);
  739.     }
  740.     else if (PtInText (thePt))
  741.     {
  742.         TEClick (thePt, (mods & shiftKey) != 0, editTE);
  743.     }
  744.  
  745.     SetScrollMax ();
  746. }
  747.  
  748.  
  749. /*
  750.     Handle key clicks in window
  751. */
  752.  
  753. static Key (c, mods)
  754. char    c;
  755. int        mods;
  756. {
  757.     SyncAllGlobals (nil);        /* sync to current port */
  758.  
  759.     if (c != enter)
  760.         TEKey (c, editTE);
  761.     AdjustDisplay ();
  762.     SetDirty (true);
  763.     if (eKey != nil)    /* report event to the host */
  764.         (*eKey) ();
  765. }
  766.  
  767.  
  768. /*
  769.     When the window comes active, highlight the scroll bar appropriately.
  770.     When the window is deactivated, un-highlight the scroll bar.
  771.     Redraw the grow box in any case.  Set the cursor (DoCursor avoids
  772.     changing it from an ibeam to an arrow back to an ibeam, in the case
  773.     where one edit window is going inactive and another is coming
  774.     active).
  775.  
  776.     Report the event to the host.
  777. */
  778.  
  779. static Activate (active)
  780. Boolean    active;
  781. {
  782.     SyncAllGlobals (nil);        /* sync to current port */
  783.  
  784.     DrawGrowBox ();
  785.     if (active)
  786.     {
  787.         TEActivate (editTE);
  788.         HiliteControl (editScroll, GetCtlMax (editScroll) > 0 ? 0 : 255);
  789.     }
  790.     else
  791.     {
  792.         TEDeactivate (editTE);
  793.         HiliteControl (editScroll, 255);
  794.     }
  795.     DoCursor (iBeamCursor);
  796.     if (eActivate != nil)    /* report event to the host */
  797.         (*eActivate) (active);
  798. }
  799.  
  800.  
  801. /*
  802.     Close box was clicked.  If user specified notify proc, call it.
  803.     Otherwise do default close operation (ask about saving if dirty,
  804.     etc.).
  805. */
  806.  
  807. static Close ()
  808. {
  809.     SyncAllGlobals (nil);        /* sync to current port */
  810.  
  811.     if (eClose != nil)
  812.         (*eClose) ();
  813.     else
  814.         (void) EWindowClose (editWind);
  815. }
  816.  
  817.  
  818. /*
  819.     Update window.  The update event might be in response to a
  820.     window resizing.  If so, move and resize the scroll bar.
  821.     The ValidRect call is done because the HideControl adds the
  822.     control bounds box to the update region - which would generate
  823.     another update event!  Since everything gets redrawn below,
  824.     the ValidRect is used to cancel the update.
  825. */
  826.  
  827. static Update (resized)
  828. Boolean    resized;
  829. {
  830. Rect            r;
  831.  
  832.     SyncGlobals (nil);        /* sync to current port */
  833.  
  834.     r = editWind->portRect;
  835.     EraseRect (&r);
  836.     if (resized)
  837.     {
  838.         HideControl (editScroll);
  839.         r = (**editScroll).contrlRect;
  840.         ValidRect (&r);
  841.         CalcScrollRect (&r);
  842.         SizeControl (editScroll, 16, r.bottom - r.top);
  843.         MoveControl (editScroll, r.left, r.top);
  844.         OverhaulDisplay (false, (**editTE).crOnly >= 0);
  845.         ShowControl (editScroll);
  846.     }
  847.     else
  848.     {
  849.         OverhaulDisplay (false, false);
  850.         DrawControls (editWind);    /* redraw scroll bar */
  851.     }
  852.  
  853.     DrawGrowBox ();
  854. }
  855.  
  856.  
  857. /*
  858.     Remove the edit window from the list, and dispose of it.
  859.     This is called by SkelRmveWind, not directly by user program.
  860.  
  861.     At this point it's too late to back out if any changes have been
  862.     made to the text.
  863.  
  864.     Since the clobber procedure is never called except for real edit
  865.     windows, and since the list must therefore be non-empty, it is
  866.     not necessary to check the legality of the window or that the
  867.     window's in the list.
  868. */
  869.  
  870. static Clobber ()
  871. {
  872. # ifndef    singleEdit
  873. register EIHandle    h, h2;
  874. # endif
  875.  
  876.     SyncGlobals (nil);                    /* sync to current port */
  877.  
  878. # ifndef    singleEdit
  879.  
  880.     if ((**ewList).editWind == editWind)    /* is it the first window in list? */
  881.     {
  882.         h2 = ewList;
  883.         ewList = (**ewList).eNext;
  884.     }
  885.     else
  886.     {
  887.         for (h = ewList; h != nil; h = h2)
  888.         {
  889.             h2 = (**h).eNext;
  890.             if ((**h2).editWind == editWind)    /* found it */
  891.             {
  892.                 (**h).eNext = (**h2).eNext;
  893.                 break;
  894.             }
  895.         }
  896.     }
  897.     DisposHandle (h2);            /* get rid of information structure */
  898.  
  899. # endif
  900.  
  901.     TEDispose (editTE);            /* toss text record */
  902.     DisposeWindow (editWind);    /* disposes of scroll bar, too */
  903.     editWind = nil;
  904.     DoCursor (iBeamCursor);
  905. }
  906.  
  907.  
  908. /*
  909.     Blink the caret and make sure the cursor's an i-beam when it's
  910.     in the non-scrollbar part of the window.
  911. */
  912.  
  913. static Idle ()
  914. {
  915.  
  916.     SyncGlobals (nil);
  917.     TEIdle (editTE);            /* blink that caret! */
  918.     DoCursor (iBeamCursor);
  919. }
  920.  
  921.  
  922. /* ---------------------------------------------------------------- */
  923. /*                        Internal File Routines                        */
  924. /* ---------------------------------------------------------------- */
  925.  
  926.  
  927. static ErrMesg (s)
  928. StringPtr    s;
  929. {
  930.     (void) FakeAlert (s, "\p", "\p", "\p", 1, 1, "\pOK", "\p", "\p");
  931. }
  932.  
  933.  
  934. /*
  935.     Save the contents of the edit window.  If there is no file bound
  936.     to the window, ask for a file name.  If askForFile is true, ask
  937.     for a name even if the window is currently bound to a file.  If
  938.     bindToFile is true, bind the window to the file written to (if
  939.     that's different than the currently bound file), and clear the
  940.     window's dirty flag.
  941.  
  942.     Return true if the file was written without error.  Return false
  943.     if (a) user was asked for name and clicked Cancel (b) there was
  944.     some error writing the file.  In the latter case, the window is
  945.     not bound to any new name given by user.
  946.  
  947.     Always returns false if the window isn't an edit window.  This
  948.     simplifies EWindowSave, EWindowSaveAs, EWindowSaveCopy.  (They
  949.     don't do the test.)
  950. */
  951.  
  952. static Boolean SaveFile (theWind, askForFile, bindToFile)
  953. WindowPtr    theWind;
  954. Boolean        askForFile;
  955. Boolean        bindToFile;
  956. {
  957. int        f;
  958. FInfo    fndrInfo;    /* finder info */
  959. SFReply    tmpFile;
  960. Handle    hText;
  961. long    count;
  962. OSErr    result;
  963. Boolean    haveNewFile = false;
  964.  
  965.     if (!IsEWindow (theWind))
  966.         return (false);
  967.  
  968.     SyncAllGlobals (theWind);
  969.     if (bound == false || askForFile)
  970.     {
  971.         SFPutFile (dlogWhere, "\pSave file as:", editFile.fName,
  972.                         nil, &tmpFile);
  973.         if (!tmpFile.good)
  974.             return (false);
  975.         else
  976.         {
  977.             haveNewFile = true;
  978.             if (GetFInfo (tmpFile.fName, tmpFile.vRefNum, &fndrInfo)
  979.                     == noErr) /* exists */
  980.             {
  981.                 if (fndrInfo.fdType != 'TEXT')
  982.                 {
  983.                     ErrMesg ("\pNot a TEXT File");
  984.                     return (false);
  985.                 }
  986.             }
  987.             else    /* doesn't exist.  create it. */
  988.             {
  989.                 if (Create (tmpFile.fName, tmpFile.vRefNum,
  990.                             creator, 'TEXT') != noErr)
  991.                 {
  992.                     ErrMesg ("\pCan't Create");
  993.                     return (false);
  994.                 }
  995.             }
  996.         }
  997.     }
  998.     
  999.     if (FSOpen (tmpFile.fName, tmpFile.vRefNum, &f) != noErr)
  1000.         ErrMesg ("\pCan't Open");
  1001.     else
  1002.     {
  1003.         DoCursor (watchCursor);
  1004.         (void) SetFPos (f, fsFromStart, 0L);
  1005.         hText = (**editTE).hText;
  1006.         HLock (hText);
  1007.         count = (**editTE).teLength;
  1008.         result = FSWrite (f, &count, *hText);
  1009.         (void) GetFPos (f, &count);
  1010.         (void) SetEOF (f, count);
  1011.         (void) FSClose (f);
  1012.         (void) FlushVol (nil, tmpFile.vRefNum);
  1013.         HUnlock (hText);
  1014.         DoCursor (iBeamCursor);
  1015.         if (result == noErr)
  1016.         {
  1017.             if (bindToFile)
  1018.             {
  1019.                 SetDirty (false);
  1020.                 if (haveNewFile)    /* name different than current */
  1021.                 {
  1022.                     SetWTitle (editWind, tmpFile.fName);
  1023.  
  1024. # ifdef    singleEdit
  1025.                     bound = true;
  1026.                     editFile = tmpFile;
  1027. # else
  1028.                     (**editInfo).bound = true;
  1029.                     (**editInfo).editFile = tmpFile;
  1030. # endif
  1031.  
  1032.                 }
  1033.             }
  1034.             return (true);
  1035.         }
  1036.         ErrMesg ("\pWrite error!");
  1037.     }
  1038.     return (false);
  1039. }
  1040.  
  1041.  
  1042. /*
  1043.     Revert to version of file saved on disk.  Doesn't check whether
  1044.     the window's really bound to a file or not, doesn't ask whether
  1045.     to really revert if the window's dirty, does no redrawing, etc.
  1046.     Just reports whether the file was read in successfully.
  1047. */
  1048.  
  1049. static Boolean Revert ()
  1050. {
  1051. Boolean    result = false;
  1052. int        f;
  1053. long    len;
  1054. Handle    h;
  1055.  
  1056.     DoCursor (watchCursor);
  1057.     if (FSOpen (editFile.fName, editFile.vRefNum, &f) != noErr)
  1058.         ErrMesg ("\pCouldn't open file");
  1059.     else
  1060.     {
  1061.         (void) GetEOF (f, &len);
  1062.         if (len >= 32000)
  1063.             ErrMesg ("\pFile is too big");
  1064.         else
  1065.         {
  1066.             h = TEGetText (editTE);
  1067.             SetHandleSize (h, len);
  1068.             HLock (h);
  1069.             (void) FSRead (f, &len, *h);
  1070.             HUnlock (h);
  1071.             (**editTE).teLength = len;
  1072.             TESetSelect (0L, 0L, editTE);    /* set caret at start */
  1073.             result = true;
  1074.             SetDirty (false);
  1075.         }
  1076.         (void) FSClose (f);
  1077.     }
  1078.     DoCursor (iBeamCursor);
  1079.     return (result);
  1080. }
  1081.  
  1082.  
  1083. /* ------------------------------------------------------------ */
  1084. /*            Lowest-level Interface (Public) Routines            */
  1085. /* ------------------------------------------------------------ */
  1086.  
  1087.  
  1088. /*
  1089.     Return true/false to indicate whether the window is really an
  1090.     edit window.
  1091. */
  1092.  
  1093. Boolean IsEWindow (theWind)
  1094. WindowPtr    theWind;
  1095. {
  1096. # ifdef    singleEdit
  1097.     return (theWind == editWind && editWind != nil);
  1098. # else
  1099.     return (GetEInfo (theWind) != nil);
  1100. # endif
  1101. }
  1102.  
  1103.  
  1104. /*
  1105.     Return true/false to indicate whether the text associated with
  1106.     the window has been changed since the last save/revert (or since
  1107.     created, if not bound to file).
  1108. */
  1109.  
  1110. Boolean IsEWindowDirty (theWind)
  1111. WindowPtr    theWind;
  1112. {
  1113. # ifndef    singleEdit
  1114. register EIHandle    eInfo;
  1115.     if ((eInfo = GetEInfo (theWind)) != nil)
  1116.         return ((**eInfo).dirty);
  1117. # else
  1118.     if (IsEWindow (theWind))
  1119.         return (dirty);
  1120. # endif
  1121.     return (false);
  1122. }
  1123.  
  1124.  
  1125. /*
  1126.     Return a handle to the TextEdit record associated with the edit
  1127.     window, or nil if it's not an edit window
  1128. */
  1129.  
  1130. TEHandle GetEWindowTE (theWind)
  1131. WindowPtr    theWind;
  1132. {
  1133. # ifndef    singleEdit
  1134. register EIHandle    eInfo;
  1135.     if ((eInfo = GetEInfo (theWind)) != nil)
  1136.         return ((**eInfo).editTE);
  1137. # else
  1138.     if (IsEWindow (theWind))
  1139.         return (editTE);
  1140. # endif
  1141.     return (nil);
  1142. }
  1143.  
  1144.  
  1145. /*
  1146.     Return true/false depending on whether the editor is bound to
  1147.     a file or not, and a copy of the file info in the second
  1148.     argument.  Pass nil for fileInfo if only want the return status.
  1149.     Returns false if it's not an edit window.
  1150. */
  1151.  
  1152. Boolean GetEWindowFile (theWind, fileInfo)
  1153. WindowPtr    theWind;
  1154. SFReply        *fileInfo;
  1155. {
  1156. # ifndef    singleEdit
  1157. register EIHandle    eInfo;
  1158.     if ((eInfo = GetEInfo (theWind)) != nil)
  1159.     {
  1160.         if (fileInfo != nil)
  1161.             *fileInfo = (**eInfo).editFile;
  1162.         return ((**eInfo).bound);
  1163.     }
  1164. # else
  1165.     if (IsEWindow (theWind))
  1166.     {
  1167.         if (fileInfo != nil)
  1168.             *fileInfo = editFile;
  1169.         return (bound);
  1170.     }
  1171. # endif
  1172.     return (false);
  1173. }
  1174.  
  1175.  
  1176. /* ---------------------------------------------------------------- */
  1177. /*                    Interface Display Routines                        */
  1178. /* ---------------------------------------------------------------- */
  1179.  
  1180.  
  1181. /*
  1182.     Install event notification procedures for an edit window.
  1183. */
  1184.  
  1185. SetEWindowProcs (theWind, pKey, pActivate, pClose)
  1186. WindowPtr    theWind;
  1187. ProcPtr        pKey;
  1188. ProcPtr        pActivate;
  1189. ProcPtr        pClose;
  1190. {
  1191. # ifndef    singleEdit
  1192. register EIHandle    eInfo;
  1193. # endif
  1194.  
  1195.     if (theWind == nil)            /* reset window creation defaults */
  1196.     {
  1197.         e_key = pKey;
  1198.         e_activate = pActivate;
  1199.         e_close = pClose;
  1200.         return;
  1201.     }
  1202.  
  1203. # ifndef    singleEdit
  1204.  
  1205.     if ((eInfo = GetEInfo (theWind)) != nil)
  1206.     {
  1207.         (**eInfo).eKey = pKey;
  1208.         (**eInfo).eActivate = pActivate;
  1209.         (**eInfo).eClose = pClose;
  1210.     }
  1211.  
  1212. # else
  1213.  
  1214.     if (IsEWindow (theWind))
  1215.     {
  1216.         eKey = pKey;
  1217.         eActivate = pActivate;
  1218.         eClose = pClose;
  1219.     }
  1220.  
  1221. # endif
  1222.  
  1223. }
  1224.  
  1225.  
  1226. /*
  1227.     Change the text display characteristics of an edit window
  1228.     and redisplay it.
  1229.  
  1230.     Scroll to home position before overhauling, because although
  1231.     the overhaul sets the viewRect to display an integral number
  1232.     of lines, there's no guarantee that the destRect offset will
  1233.     also be integral except at home position.  Clipping is set to
  1234.     an empty rect so the scroll doesn't show.
  1235. */
  1236.  
  1237. SetEWindowStyle (theWind, font, size, wrap, just)
  1238. WindowPtr    theWind;
  1239. int            font;
  1240. int            size;
  1241. int            wrap;
  1242. int            just;
  1243. {
  1244. GrafPtr                savePort;
  1245. FontInfo            f;
  1246. register TEHandle    te;
  1247. Rect                r;
  1248. int                    oldWrap;
  1249.  
  1250.     if (theWind == nil)            /* reset window creation defaults */
  1251.     {
  1252.         e_font = font;
  1253.         e_size = size;
  1254.         e_wrap = wrap;
  1255.         e_just = just;
  1256.         return;
  1257.     }
  1258.  
  1259.     if (IsEWindow (theWind))
  1260.     {
  1261.         GetPort (&savePort);
  1262.         SyncGlobals (theWind);    /* sync and set port */
  1263.         te = editTE;
  1264.         ScrollToHome ();
  1265.  
  1266.         oldWrap = (**te).crOnly;
  1267.         (**te).crOnly = wrap;    /* set word wrap */
  1268.         TESetJust (just, te);    /* set justification */
  1269.  
  1270.         TextFont (font);         /* set the font and point size */
  1271.         TextSize (size);        /* of text record */
  1272.         GetFontInfo (&f);
  1273.         (**te).lineHeight = f.ascent + f.descent + f.leading;
  1274.         (**te).fontAscent = f.ascent;
  1275.         (**te).txFont = font;
  1276.         (**te).txSize = size;
  1277.  
  1278.         OverhaulDisplay (true, (oldWrap >= 0 || wrap >= 0));
  1279.  
  1280.         SetPort (savePort);
  1281.     }
  1282. }
  1283.  
  1284.  
  1285. /*
  1286.     Redo display.  Does not save current port.  This is used by hosts
  1287.     that mess with the text externally to TransEdit.  The arguments
  1288.     determine whether the text is scrolled to show the line with the
  1289.     caret, whether the lineStarts are recalculated, and whether the
  1290.     text should be marked dirty or not.
  1291. */
  1292.  
  1293. EWindowOverhaul (theWind, showCaret, recalc, dirty)
  1294. WindowPtr    theWind;
  1295. Boolean        showCaret;
  1296. Boolean        recalc;
  1297. Boolean        dirty;
  1298. {
  1299.     if (IsEWindow (theWind))
  1300.     {
  1301.         SyncGlobals (theWind);
  1302.         OverhaulDisplay (showCaret, recalc);
  1303.         DrawControls (editWind);
  1304.         SetDirty (dirty);
  1305.     }
  1306. }
  1307.  
  1308.  
  1309. /* ---------------------------------------------------------------- */
  1310. /*                        Menu Interface Routine                        */
  1311. /* ---------------------------------------------------------------- */
  1312.  
  1313.  
  1314. /*
  1315.     Do Edit menu selection.  This is only valid if an edit
  1316.     window is frontmost.
  1317. */
  1318.  
  1319. EWindowEditOp (item)
  1320. int        item;
  1321. {
  1322.  
  1323.     if (!IsEWindow (FrontWindow ()))
  1324.         return;                /* host messed up */
  1325.  
  1326.     SyncGlobals (FrontWindow ());
  1327.  
  1328.     switch (item)
  1329.     {
  1330.  
  1331. /*
  1332.     cut selection, put in TE Scrap, clear clipboard and put
  1333.     TE scrap in it
  1334. */
  1335.         case cut:
  1336.         {
  1337.             TECut (editTE);
  1338.             (void) ZeroScrap ();
  1339.             (void) TEToScrap ();
  1340.             break;
  1341.         }
  1342. /*
  1343.     copy selection to TE Scrap, clear clipboard and put
  1344.     TE scrap in it
  1345. */
  1346.         case copy:
  1347.         {
  1348.             TECopy (editTE);
  1349.             (void) ZeroScrap ();
  1350.             (void) TEToScrap ();
  1351.             break;
  1352.         }
  1353. /*
  1354.     get clipboard into TE scrap, put TE scrap into edit record
  1355. */
  1356.         case paste:
  1357.         {
  1358.             (void) TEFromScrap ();
  1359.             TEPaste (editTE);
  1360.             break;
  1361.         }
  1362. /*
  1363.     delete selection without putting into TE scrap or clipboard
  1364. */
  1365.         case clear:
  1366.         {
  1367.             (void) TEDelete (editTE);
  1368.             break;
  1369.         }
  1370.  
  1371.     }
  1372.     AdjustDisplay ();
  1373.     SetDirty (true);
  1374. }
  1375.  
  1376.  
  1377. /* ---------------------------------------------------------------- */
  1378. /*                        Interface File Routines                        */
  1379. /* ---------------------------------------------------------------- */
  1380.  
  1381.  
  1382. /*
  1383.     Set file creator for any files created by TransEdit
  1384. */
  1385.  
  1386. SetEWindowCreator (creat)
  1387. OSType    creat;
  1388. {
  1389.     creator = creat;
  1390. }
  1391.  
  1392.  
  1393.  
  1394. /*
  1395.     Save the contents of the given window
  1396. */
  1397.  
  1398. Boolean EWindowSave (theWind)
  1399. WindowPtr    theWind;
  1400. {
  1401.     return (SaveFile (theWind,    /* window to save */
  1402.                       false,    /* don't ask for file if have one */
  1403.                       true));    /* bind to new file if one given */
  1404. }
  1405.  
  1406.  
  1407. /*
  1408.     Save the contents of the given window under a new name
  1409.     and bind to that name.
  1410. */
  1411.  
  1412. Boolean EWindowSaveAs (theWind)
  1413. WindowPtr    theWind;
  1414. {
  1415.     return (SaveFile (theWind,    /* window to save */
  1416.                       true,        /* ask for file even if have one */
  1417.                       true));    /* bind to new file if one given */
  1418. }
  1419.  
  1420.  
  1421. /*
  1422.     Save the contents of the given window under a new name, but
  1423.     don't bind to the name.
  1424. */
  1425.  
  1426. Boolean EWindowSaveCopy (theWind)
  1427. WindowPtr    theWind;
  1428. {
  1429.     return (SaveFile (theWind,    /* window to save */
  1430.                       true,        /* ask for file even if have one */
  1431.                       false));    /* don't bind to file */
  1432. }
  1433.  
  1434.  
  1435. /*
  1436.     Close the window.  If it's dirty and is either bound to a file
  1437.     or (if not bound) has some text in it, ask about saving it first,
  1438.     giving user option of saving changes, tossing them, or
  1439.     cancelling altogether.
  1440.  
  1441.     Return true if the file was saved and the window closed, false if
  1442.     user cancelled or there was an error.
  1443. */
  1444.  
  1445. Boolean EWindowClose (theWind)
  1446. WindowPtr    theWind;
  1447. {
  1448.     if (IsEWindow (theWind) == false)
  1449.         return (false);
  1450.  
  1451.     SyncAllGlobals (theWind);
  1452.     if ( (bound || (**editTE).teLength > 0) && dirty)
  1453.     {
  1454.         switch (FakeAlert ("\pSave changes to \"", editFile.fName,
  1455.                 "\p\"?", "\p", 3, 3,
  1456.                 "\pCancel", "\pDiscard", "\pSave"))    /* ask whether to save */
  1457.         {
  1458.  
  1459.             case 1:            /* cancel Close */
  1460.                 return (false);
  1461.  
  1462.             case 2:            /* toss changes */
  1463.                 break;
  1464.  
  1465.             case 3:
  1466.                 if (SaveFile (editWind,    /* window to save */
  1467.                               false,    /* don't ask for name */
  1468.                               false)    /* don't bind to name */
  1469.                         == false)
  1470.                     return (false);    /* cancelled or error - cancel Close */
  1471.                 break;
  1472.         }
  1473.     }
  1474.     SkelRmveWind (editWind);
  1475.     return (true);
  1476. }
  1477.  
  1478.  
  1479. /*
  1480.     Revert to saved version of file on disk.  theWind must be an edit
  1481.     window, and must be bound to a file.  Returns false if one of these
  1482.     conditions is not met, or if they are met but there was an error
  1483.     reading the file.
  1484.  
  1485.     The window need not be dirty, but if it is, the user is asked
  1486.     whether to really revert.
  1487. */
  1488.  
  1489. Boolean EWindowRevert (theWind)
  1490. WindowPtr    theWind;
  1491. {
  1492.     if (!IsEWindow (theWind))
  1493.         return (false);
  1494.     SyncAllGlobals (theWind);
  1495.     if (!bound)
  1496.         return (false);        /* no file to revert to */
  1497.     if (dirty)
  1498.     {
  1499.         if (FakeAlert ("\p\"", editFile.fName,
  1500.                 "\p\" has been changed.  Really revert?",
  1501.                 "\p", 2, 1, "\pCancel", "\pRevert", "\p") == 1)
  1502.             return (false);
  1503.     }
  1504.     if (Revert () == false)
  1505.         return (false);
  1506.     ScrollToHome ();
  1507.     OverhaulDisplay (true, true);
  1508.     DrawControls (editWind);
  1509.     ValidRect (&editWind->portRect);
  1510.     return (true);
  1511. }
  1512.  
  1513.  
  1514. /* ---------------------------------------------------------------- */
  1515. /*            Interface Initialization/Termination Routines            */
  1516. /* ---------------------------------------------------------------- */
  1517.  
  1518.  
  1519. /*
  1520.     Initialize the window and associated data structures.
  1521.     Return window pointer or nil if some sort of error.
  1522.  
  1523.     Preserves the current port.
  1524. */
  1525.  
  1526. WindowPtr NewEWindow (bounds, title, visible, behind,
  1527.                             goAway, refNum, bindToFile)
  1528. Rect        *bounds;
  1529. StringPtr    title;
  1530. Boolean        visible;
  1531. WindowPtr    behind;
  1532. Boolean        goAway;
  1533. long        refNum;
  1534. Boolean        bindToFile;
  1535. {
  1536. GrafPtr        savePort;
  1537. Rect        r;
  1538. OSType        type = 'TEXT';
  1539. Str255        s, s2;
  1540. StringPtr    tPtr;
  1541.  
  1542. # ifndef    singleEdit
  1543. register EIHandle    eInfo;
  1544. # endif
  1545.  
  1546. # ifdef    singleEdit
  1547.  
  1548.     if (editWind != nil)    /* allow only one window at a time */
  1549.         return (nil);
  1550.  
  1551. # endif
  1552.  
  1553. /*
  1554.     If supposed to bind to file, ask for name.  Return without doing
  1555.     anything if Cancel button clicked.
  1556. */
  1557.  
  1558.     if (bindToFile)
  1559.     {
  1560.         SFGetFile (dlogWhere, "\p", nil, 1, &type, nil, &editFile);
  1561.         if (!editFile.good)
  1562.             return (nil);
  1563.     }
  1564.     bound = bindToFile;
  1565.  
  1566. /*
  1567.     Create window and install handler.  Set window title:  If window is
  1568.     to be bound to file, use name of file.  Otherwise use any title that
  1569.     was passed in.  If nil was passed, use a default name ("Untitled nnn").
  1570.     Also copy the name into the file info structure even if the window is
  1571.     unbound, because the Save operations expect to find it there as the
  1572.     most likely name to use if the window is untitled.
  1573.  
  1574.     Save and restore port, because it gets reset by the rest of the
  1575.     initialization code.
  1576. */
  1577.  
  1578.     if (bound)
  1579.         tPtr = editFile.fName;
  1580.     else
  1581.     {
  1582.         if (title != nil)
  1583.             tPtr = title;
  1584.         else
  1585.         {
  1586.  
  1587. # ifndef    singleEdit
  1588.             BlockMove ("\pUntitled ", s, 10L);
  1589.             NumToString ((long) ++windID, s2);
  1590.             BlockMove (&s2[1], &s[10], (long) s2[0]);
  1591.             s[0] += s2[0];
  1592.             tPtr = s;
  1593. # else
  1594.             tPtr = (StringPtr) "\pUntitled";
  1595. # endif
  1596.  
  1597.         }
  1598.         BlockMove (tPtr, editFile.fName, (long) (tPtr[0] + 1));
  1599.     }
  1600.  
  1601.     editWind = NewWindow (nil, bounds, tPtr, false, documentProc,
  1602.                                 behind, goAway, refNum);
  1603.  
  1604.     GetPort (&savePort);
  1605.     SkelWindow (editWind,    /* the window */
  1606.                 Mouse,        /* mouse click handler */
  1607.                 Key,        /* key click handler */
  1608.                 Update,        /* window updating procedure */
  1609.                 Activate,    /* window activate/deactivate procedure */
  1610.                 Close,        /* window close procedure */
  1611.                 Clobber,    /* window disposal procedure */
  1612.                 Idle,        /* idle proc */
  1613.                 true);        /* idle only when frontmost */
  1614.  
  1615.  
  1616. /*
  1617.     Build the scroll bar.
  1618. */
  1619.  
  1620.     CalcScrollRect (&r);
  1621.     editScroll = NewControl (editWind, &r, "\p", true, 0, 0, 0,
  1622.                                 scrollBarProc, 0L);
  1623.  
  1624.  
  1625.  
  1626. /*
  1627.     Create the TE record used for text display.  Use default
  1628.     characteristics.
  1629. */
  1630.  
  1631.     GetEditRect (&r);
  1632.     editTE = TENew (&r, &r);
  1633.     SetClikLoop (AutoScroll, editTE);            /* set autoscroll proc */
  1634.  
  1635.  
  1636. # ifndef    singleEdit
  1637.  
  1638. /*
  1639.     Get new information structure, attach to list of known edit
  1640.     windows.
  1641. */
  1642.  
  1643.     eInfo = New (EditInfo);
  1644.     editInfo = eInfo;
  1645.     (**eInfo).eNext = ewList;
  1646.     ewList = eInfo;
  1647.     (**eInfo).editWind = editWind;
  1648.     (**eInfo).scroll = editScroll;
  1649.     (**eInfo).editTE = editTE;
  1650.     (**eInfo).bound = bound;
  1651.     (**eInfo).editFile = editFile;
  1652.  
  1653. # endif
  1654.  
  1655. /*
  1656.     Install default event notification procedures, font characteristics.
  1657. */
  1658.  
  1659.     SetEWindowProcs (editWind, e_key, e_activate, e_close);
  1660.     SetEWindowStyle (editWind, e_font, e_size, e_wrap, e_just);
  1661.     SetDirty (false);
  1662.  
  1663. /*
  1664.     If supposed to read file, do so.  Check the return value of
  1665.     Revert and toss the window if there was an error.
  1666. */
  1667.  
  1668.     if (bindToFile && Revert () == false)
  1669.     {
  1670.         SkelRmveWind (editWind);
  1671.         SetPort (savePort);
  1672.         return (nil);
  1673.     }
  1674.  
  1675. /*
  1676.     Show window if specified as visible, and return a pointer to it.
  1677. */
  1678.  
  1679.     SyncGlobals (editWind);
  1680.     OverhaulDisplay (true, true);
  1681.     if (visible)
  1682.         ShowWindow (editWind);
  1683.     SetPort (savePort);
  1684.     return (editWind);
  1685. }
  1686.  
  1687.  
  1688. /*
  1689.     Look through the list of windows, shutting down all the edit
  1690.     windows.  If any window is dirty, ask user about saving it first.
  1691.     If the user cancels on any such request, ClobberEWindows returns
  1692.     false.  If all edit windows are shut down, return true.  It is
  1693.     then safe for the host to exit.
  1694.  
  1695.     When a window *is* shut down, have to start looking through the
  1696.     window list again, since theWind no longer points anywhere
  1697.     meaningful.
  1698. */
  1699.  
  1700. Boolean ClobberEWindows ()
  1701. {
  1702. WindowPtr    theWind;
  1703.  
  1704.     for (;;)
  1705.     {
  1706.         for (theWind = FrontWindow ();
  1707.                 theWind != nil;
  1708.                     theWind = (WindowPtr) ((WindowPeek) theWind)->nextWindow)
  1709.         {
  1710.             if (IsEWindow (theWind))
  1711.                 break;
  1712.         }
  1713.         if (theWind == nil)
  1714.             return (true);        /* all edit windows are shut down */
  1715.  
  1716.         if (theWind != FrontWindow ())
  1717.         {
  1718.             SelectWindow (theWind);
  1719.             ShowWindow (theWind);
  1720.             EWindowOverhaul (theWind, false, false, IsEWindowDirty (theWind));
  1721.             SetPort (theWind);
  1722.             ValidRect (&theWind->portRect);
  1723.         }
  1724.  
  1725.         if (EWindowClose (theWind) == false)
  1726.             return (false);        /* cancel or error */
  1727.     }
  1728. }
  1729.